package com.pri.chat.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.NoticeUnreadNumber;
import com.netease.nim.uikit.net.entity.SystemMsgBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.MainActivity;
import com.pri.chat.MainDaoHangActivity;
import com.pri.chat.R;
import com.pri.chat.adapter.SystemMsgAdapter;
import com.pri.chat.adapter.ZanMsgAdapter;
import com.pri.chat.application.APP;
import com.pri.chat.base.BaseActivity;
import com.pri.chat.bean.KeyAndValueEntity;
import com.pri.chat.database.greendao.KeyAndValueEntityDao;
import com.pri.chat.utils.Des3Util;
import com.pri.chat.utils.RecycleViewDivider;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author
 * @time 2020/5/9
 */
public class NoticePushMsgActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.rec_main)
    RecyclerView recMain;
    @BindView(R.id.none)
    ImageView none;
    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_notice_push_msg_layout;
    }

    @Override
    protected void initView() {
        setTitle("系统提醒");
        srlAll.setEnableLoadmore(true);
        initLayout();
        initSrl();
    }

    private List<SystemMsgBean> mList;
    private SystemMsgAdapter mAdapter;
    private int page = 1;
    private boolean isShowDialog = true;

    private void initLayout() {
        mList = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recMain.setLayoutManager(linearLayoutManager);
        mAdapter = new SystemMsgAdapter(R.layout.item_system_msg, mList);
        recMain.setAdapter(mAdapter);
//        mAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        SubscriberOnNextListener<BaseBean<List<SystemMsgBean>>> onNextListener = baseBean -> {
            if (baseBean.getData().isEmpty() && page == 1) {
                none.setVisibility(View.VISIBLE);
                mAdapter.notifyDataSetChanged();
                srlAll.setEnableLoadmore(false);
            } else {
                none.setVisibility(View.INVISIBLE);
                mList.addAll(baseBean.getData());
                mAdapter.notifyDataSetChanged();
                if(baseBean.getData().size()<10){
                    srlAll.setEnableLoadmore(false);
                }
            }
            KeyAndValueEntity kavObj = APP.getKeyAndValueEntityDao().queryBuilder().where(KeyAndValueEntityDao.Properties.TypeKey.eq("unreadnum")).build().list().get(0);
            NoticeUnreadNumber unreadNumber = new Gson().fromJson(kavObj.getValue(),NoticeUnreadNumber.class);
            unreadNumber.setXttzNum(0);
            kavObj.setValue(new Gson().toJson(unreadNumber));
            APP.getKeyAndValueEntityDao().update(kavObj);
        };
        Map<String, Object> dtModel = new HashMap<>();//登录上传参数
        dtModel.put("memberId", SharedHelper.readId(this));
        dtModel.put("pageIndex", page);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(dtModel));
        HttpMethods.getInstance().listPushMsg(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);
    }

    private void initSrl() {
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh_layout.finishRefresh(1000);
            refresh();
        });
        srlAll.setOnLoadmoreListener(refresh_layout -> {
            refresh_layout.finishLoadmore(1000);
            isShowDialog = false;
            page++;
            initData();
        });
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        SystemMsgBean sysData = mList.get(position);
        //0--系统消息；1--充值成功；2--提现成功；3--提现失败
        if ("0".equals(sysData.getType()) || "70".equals(sysData.getType())) {
            //动态
            Bundle bundle = new Bundle();
            bundle.putString("id", sysData.getMsgId());
            RxActivityTool.skipActivity(mContext, DTmsgActivity.class, bundle);
        } else {
            Bundle bundle = new Bundle();
            bundle.putString("id", sysData.getMsgId());
            RxActivityTool.skipActivity(mContext, VedioMsgActivity.class, bundle);
        }
    }

    private void refresh() {
        page = 1;
        isShowDialog = false;
        mList.clear();
        mAdapter.notifyDataSetChanged();
        initData();
    }
}
